package com.alog.storage.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.alog.storage.Contants;
import com.alog.storage.R;
import com.alog.storage.StorageApplication;
import com.alog.storage.ui.fragment.StorageManagerFragment;
import com.alog.storage.ui.fragment.StorageFindFragment;
import com.alog.storage.ui.fragment.MyFragment;

/**
 * 首页TAB框架界面
 */
public class MainTabActivity extends BaseActivity {
    private static final String PREVIOUS_MODULEID = "previous_moduleid";
    private static final int MSG_SWITCH_MODULE = 11;
    private FragmentManager mFragmentManager;
    private LinearLayout mManagerLayout, mFindLayout, mMyLayout;
    private int mSelectedModule;
    private long mExitTime;
    private String mCurrentMoudle = "";
    private boolean isFirstOnresume = true;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_SWITCH_MODULE:
                    switchModule(msg.arg1);
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        isSlideBack = false;
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.push, R.anim.push);
        setContentView(R.layout.maintab);
        mFragmentManager = getSupportFragmentManager();
        findViews();
        setOnClickListener();
        if (savedInstanceState != null) {
            mSelectedModule = savedInstanceState.getInt(PREVIOUS_MODULEID, R.id.ll_maintab_manager);
            hideAllFragments();
        } else {
            mSelectedModule = getIntent().getIntExtra("module", R.id.ll_maintab_find);
        }
        switchModule(mSelectedModule);
    }

    private void findViews() {
        mManagerLayout = (LinearLayout) findViewById(R.id.ll_maintab_manager);
        mFindLayout = (LinearLayout) findViewById(R.id.ll_maintab_find);
        mMyLayout = (LinearLayout) findViewById(R.id.ll_maintab_my);
    }

    private void setOnClickListener() {
        mManagerLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedModule != R.id.ll_maintab_manager) {
                    switchModule(R.id.ll_maintab_manager);
                    mExitTime = 0;
                }
            }
        });
        mFindLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedModule != R.id.ll_maintab_find) {
                    switchModule(R.id.ll_maintab_find);
                    mExitTime = 0;
                }
            }
        });
        mMyLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedModule != R.id.ll_maintab_my) {
                    if (!/*AccountPresenter.isLogin(mContext)*/true) {
                        Intent i = new Intent();
                        i.setClass(context, LoginActivity.class);
                        startActivityForResult(i, Contants.ACTIVITY_CODE_LOGIN);
                    }
                    switchModule(R.id.ll_maintab_my);
                    mExitTime = 0;
                }
            }
        });
    }

    public void switchModule(int moudleId) {
        String frontTag = "fragment" + moudleId;
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        Fragment currentFragment = mFragmentManager.findFragmentByTag(mCurrentMoudle);
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        Fragment frontFragment = mFragmentManager.findFragmentByTag(frontTag);
        if (frontFragment != null) {
            transaction.show(frontFragment);
        } else {
            frontFragment = makeFragment(moudleId);
            transaction.add(R.id.rl_maintab_fragment, frontFragment, frontTag);
        }
        mCurrentMoudle = frontTag;
        transaction.commit();
        setSelecteModule(moudleId);
    }

    private Fragment makeFragment(int moudleId) {
        Fragment fragment = null;
        switch (moudleId) {
            case R.id.ll_maintab_manager:
                fragment = new StorageManagerFragment();
                break;
            case R.id.ll_maintab_find:
                fragment = new StorageFindFragment();
                break;
            case R.id.ll_maintab_my:
                fragment = new MyFragment();
                break;
        }
        return fragment;
    }

    private void setSelecteModule(int moudleId) {
        mSelectedModule = moudleId;
        mManagerLayout.setSelected(false);
        mFindLayout.setSelected(false);
        mMyLayout.setSelected(false);

        switch (moudleId) {
            case R.id.ll_maintab_manager:
                mManagerLayout.setSelected(true);
                break;
            case R.id.ll_maintab_find:
                mFindLayout.setSelected(true);
                break;
            case R.id.ll_maintab_my:
                mMyLayout.setSelected(true);
                break;
        }
    }

    private void hideAllFragments() {
        Fragment managerFragment = mFragmentManager.findFragmentByTag("fragment" + R.id.ll_maintab_manager);
        Fragment findFragment = mFragmentManager.findFragmentByTag("fragment" + R.id.ll_maintab_find);
        Fragment myFragment = mFragmentManager.findFragmentByTag("fragment" + R.id.ll_maintab_my);
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (managerFragment != null) {
            transaction.hide(managerFragment);
        }
        if (findFragment != null) {
            transaction.hide(findFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
        transaction.commit();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        int frontModule = intent.getIntExtra("module", R.id.ll_maintab_manager);
        Message s = mHandler.obtainMessage();
        s.what = MSG_SWITCH_MODULE;
        s.arg1 = frontModule;
        mHandler.sendMessage(s);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(PREVIOUS_MODULEID, mSelectedModule);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isFirstOnresume) {
            isFirstOnresume = false;
        } else {
            Fragment currentFragment = mFragmentManager.findFragmentByTag(mCurrentMoudle);
            if (currentFragment != null) currentFragment.onHiddenChanged(false);

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Fragment currentFragment = mFragmentManager.findFragmentByTag(mCurrentMoudle);
        if (currentFragment != null) currentFragment.onHiddenChanged(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Contants.ACTIVITY_CODE_NET_SETTING) {
            switchModule(R.id.ll_maintab_manager);
            mExitTime = 0;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - mExitTime > 2000) {
            mExitTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
        } else {
            StorageApplication.exit();
        }
    }

}
